/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.bo.ActorBO;
import org.ipti.br.tag.bo.AdditionalInfoBO;
import org.ipti.br.tag.bo.HumanBO;
import org.ipti.br.tag.bo.LoginBO;
import org.ipti.br.tag.bo.PersonageBO;
import org.ipti.br.tag.bo.UnityBO;
import org.ipti.br.tag.entity.Actor;
import org.ipti.br.tag.entity.AdditionalInfo;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.ipti.br.tag.bean.lazy.LazyActorDataModel;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Person;

/**
 *
 * @author israel
 */
@Named(value= "humanSearch")
@SessionScoped
public class HumanSearchBean extends AppBaseBean {

    @Inject
    private ActorBO actorBO;
    @Inject
    private AdditionalInfoBO additionalInfoBO;
    @Inject
    private HumanBO humanBO;
    @Inject
    private LoginBO loginBO;
    @Inject
    private PersonageBO personageBO;
    @Inject
    private UnityBO unityBO;
    @Inject
    private SessionBean sessionBean;
    
    private Actor actor;
    private Long personageID;
    private Date deactivation;
    private LazyActorDataModel actors;
    
    private LazyActorDataModel filteredActors;

    public HumanSearchBean() {
    }

    public String getPersonageName() {
        return getPersonageBO().find(personageID).getName();
    }
    
    public String getHumanName(Person person) {
        if (Human.class.isInstance(person)) {
            return ((Human) person).getName();
        } else {
            return "";
        }
    }

    public void init() {
        actors = new LazyActorDataModel(actorBO.getActorDAO(),
                unityBO.unityChildrenPerOrganization2(sessionBean.getLoggedActor().getUnity().getId(),
                personageBO.find(personageID).getOrganization().getId()),
                personageID);
    }

    public void preEdit() {
        if (actor.getDeactivated() != null) {
            deactivation = actor.getDeactivated().getTime();
        }
    }
    

    public void edit() {
        try {
            Actor actorAux = actorBO.find(actor.getId());
            actorAux.setActivated(null);
            actorAux.setDeactivated(null);
            actorBO.edit(actorAux);

            if (deactivation == null) {
                actor.setDeactivated(null);
            } else {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(deactivation);
                actor.setDeactivated(calendar);
            }
            actorAux.setActivated(actor.getActivated());
            actorAux.setDeactivated(actor.getDeactivated());
            actorBO.edit(actorAux);
            actor = null;
            deactivation = null;
            //init();
            addInfoMessage("humanSearchForm", "Operação realizada com sucesso", null);
        } catch (Exception ex) {
            addErrorMessage("humanSearchForm", "Um erro foi encontrado", null);
        }
    }

    public void delete() {
        try {
            actorBO.remove(actor);
            addInfoMessage("humanSearchForm", "Operação realizada com sucesso", null);
        } catch (Exception ex) {
            addErrorMessage("humanSearchForm", "Um erro inesperado foi encontrado", null);
        }
        init();
    }
    
    public List<AdditionalInfo> personAdditionalInfo(Long personID) {
        return additionalInfoBO.findAddInfosPerPerson(personID);
    }

    /*
     * Getters and Setters
     */
    public ActorBO getActorBO() {
        return actorBO;
    }

    public void setActorBO(ActorBO actorBO) {
        this.actorBO = actorBO;
    }

    public AdditionalInfoBO getAdditionalInfoBO() {
        return additionalInfoBO;
    }

    public void setAdditionalInfoBO(AdditionalInfoBO additionalInfoBO) {
        this.additionalInfoBO = additionalInfoBO;
    }

    public HumanBO getHumanBO() {
        return humanBO;
    }

    public void setHumanBO(HumanBO humanBO) {
        this.humanBO = humanBO;
    }

    public LoginBO getLoginBO() {
        return loginBO;
    }

    public void setLoginBO(LoginBO loginBO) {
        this.loginBO = loginBO;
    }

    public PersonageBO getPersonageBO() {
        return personageBO;
    }

    public void setPersonageBO(PersonageBO personageBO) {
        this.personageBO = personageBO;
    }

    public UnityBO getUnityBO() {
        return unityBO;
    }

    public void setUnityBO(UnityBO unityBO) {
        this.unityBO = unityBO;
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public Long getPersonageID() {
        return personageID;
    }

    public void setPersonageID(Long personageID) {
        this.personageID = personageID;
    }

    public Actor getActor() {
        return actor;
    }

    public void setActor(Actor actor) {
        this.actor = actor;
    }
    
    public void setFilteredActors(LazyActorDataModel filteredActors){
        this.filteredActors = filteredActors;
        
    }
    
    
    public LazyActorDataModel getFilteredActors(){
        return filteredActors;
    }
    
    public LazyActorDataModel getActors() {
        return actors;
    }

    public void setActors(LazyActorDataModel actors) {
        this.actors = actors;
    }

    public Date getDeactivation() {
        return deactivation;
    }

    public void setDeactivation(Date deactivation) {
        this.deactivation = deactivation;
    }
    
    

}
